package me.send.produce.data.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import me.send.business.base.Pager;
import me.send.data.service.pojo.Manager;
import me.send.data.service.pojo.UserRechargeList;
import me.send.data.service.pojo.base.DatatablesViewPage;
import me.send.data.service.service.TreeService;
import me.send.data.service.service.UserRechargeListService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "user")
public class UserRechargeListController {

	@Autowired
	private UserRechargeListService userRechargeListService;

	@Autowired
	private TreeService treeService;

	   

	@RequestMapping(value = "recharge/list")
	public ModelAndView recharge_list(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView(
				"stage/user/user_recharge_list");
		modelAndView.addObject("LoadTree",
				treeService.main_tree(0, request, "user/recharge/list"));
		modelAndView.addObject("module", "充值金额列表");
		return modelAndView;
	}

	
	@RequestMapping(value="recharge/page", method=RequestMethod.GET)  
    @ResponseBody  
    public DatatablesViewPage<UserRechargeList> recharge_page(HttpServletRequest request){  
//获取分页控件的信息  
        String start = request.getParameter("start");  
        String length = request.getParameter("length"); 
        System.out.println(start);
        System.out.println(length);
//获取前台额外传递过来的查询条件  
//        SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm");
//        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String listCode = request.getParameter("listCode");  
        String status = request.getParameter("status");  
        Integer paramStatus=null;
        if(status==null||(status!=null&&"".equals(status.trim()))){
        	status=null;
        }else {
        	paramStatus=Integer.parseInt(status);
        }
        if(listCode==null||(listCode!=null&&"".equals(listCode.trim()))){
        	listCode=null;
        }
        
        Pager page=new Pager();
        Integer row=Integer.parseInt(length);
        page.setRows(row);
        Integer startInt=Integer.parseInt(start);
        Integer p=(startInt/row)+1;
        page.setPage(p);
        
                //随便组织的查询结果  
        List<UserRechargeList> list=userRechargeListService.list(listCode,paramStatus,page); 
        Integer count=userRechargeListService.count(listCode,paramStatus);
        DatatablesViewPage<UserRechargeList> view = new DatatablesViewPage<UserRechargeList>();  
        view.setiTotalDisplayRecords(count);  
        view.setiTotalRecords(count);  
          
        view.setAaData(list);  
        return view;  
    } 
	
	/**
	 * 删除
	 * @param listId
	 * @return
	 */
	@RequestMapping(value = "recharge/delete")
	@ResponseBody
	public void delete(int listId) {
		userRechargeListService.deteleBylistId(listId);;
	}
	
	/**
	 * 修改
	 * @param listId
	 * @return
	 */
	@RequestMapping(value = "recharge/edit")
	@ResponseBody
	public ModelAndView update(HttpServletRequest request) {
		Long listId=Long.parseLong(request.getParameter("listId"));
		String listCode=request.getParameter("listCode");
		String listDesc=request.getParameter("listDesc");
		BigDecimal amount=new BigDecimal(request.getParameter("amount"));
		BigDecimal giveAmount=new BigDecimal(request.getParameter("giveAmount"));
		Integer status=Integer.parseInt(request.getParameter("status"));
		String activeTimeBegin=request.getParameter("activeTimeBegin");
		String activeTimeEnd=request.getParameter("activeTimeEnd");
		UserRechargeList recharge=new UserRechargeList();
		recharge.setAmount(amount);
		recharge.setGiveAmount(giveAmount);
		recharge.setListCode(listCode);
		recharge.setStatus(status);
		recharge.setListId(listId);
		recharge.setListDesc(listDesc);
		recharge.setActiveTimeBegin(activeTimeBegin);
		recharge.setActiveTimeEnd(activeTimeEnd);
		userRechargeListService.update(recharge);
		ModelAndView modelAndView = new ModelAndView(
				"stage/user/user_recharge_list");
		modelAndView.addObject("LoadTree",
				treeService.main_tree(0, request, "user/recharge/list"));
		modelAndView.addObject("module", "充值金额列表");
		return modelAndView;
	}
	/**
	 * 添加
	 * @param listId
	 * @return
	 */
	@RequestMapping(value = "recharge/add")
	@ResponseBody
	public ModelAndView insert(HttpServletRequest request) {
		String listCode=request.getParameter("listCode");
		String listDesc=request.getParameter("listDesc");
		BigDecimal amount=new BigDecimal(request.getParameter("amount"));
		BigDecimal giveAmount=new BigDecimal(request.getParameter("giveAmount"));
		Integer status=Integer.parseInt(request.getParameter("status"));
		String activeTimeBegin=request.getParameter("activeTimeBegin");
		String activeTimeEnd=request.getParameter("activeTimeEnd");
		UserRechargeList recharge=new UserRechargeList();
		recharge.setAmount(amount);
		recharge.setGiveAmount(giveAmount);
		recharge.setListCode(listCode);
		recharge.setStatus(status);
		recharge.setListDesc(listDesc);
		recharge.setActiveTimeBegin(activeTimeBegin);
		recharge.setActiveTimeEnd(activeTimeEnd);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		recharge.setCreateTime(sdf.format(new Date()));
		Manager manager =(Manager)request.getSession().getAttribute("currentUser");
		recharge.setCreateUser(manager.getNickName());
		userRechargeListService.insert(recharge);
		ModelAndView modelAndView = new ModelAndView(
				"stage/user/user_recharge_list");
		modelAndView.addObject("LoadTree",
				treeService.main_tree(0, request, "user/recharge/list"));
		modelAndView.addObject("module", "充值金额列表");
		return modelAndView;
	}
	
	@RequestMapping(value = "recharge/edit/detail")
	public ModelAndView recharge_edit_detail(HttpServletRequest request,int listId) {
		ModelAndView modelAndView = new ModelAndView(
				"stage/user/user_recharge_edit");
		UserRechargeList recharge=userRechargeListService.queryBylistId(listId);
				
		modelAndView.addObject("recharge",recharge);
		modelAndView.addObject("LoadTree",
				treeService.main_tree(0, request, "user/recharge/edit/detail"));
		modelAndView.addObject("module", "充值金额编辑");
		return modelAndView;
	}
	@RequestMapping(value = "recharge/add/detail")
	public ModelAndView recharge_add_detail(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView(
				"stage/user/user_recharge_add");
		UserRechargeList recharge=new UserRechargeList();
		
		modelAndView.addObject("recharge",recharge);
		modelAndView.addObject("LoadTree",
				treeService.main_tree(0, request, "user/recharge/add/detail"));
		modelAndView.addObject("module", "充值金额添加");
		return modelAndView;
	}
	
}





